﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace QXJ
{
    static class Program
    {

        static bool IsAdminPrivi = false;
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main(string[] Args)
        {
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();  


            #region one app run
            //只启动一个例子
            System.Threading.Mutex mutex = new System.Threading.Mutex(false, "Hello Fuck");
            bool hasInstance = !mutex.WaitOne(0, false);
            if (hasInstance)
            {
                MessageBox.Show("系统已经启动,请等待...", "提示" + DateTime.Now);
                return;
            }
            #endregion

            Application.EnableVisualStyles();
            if (IsAdminPrivi)
            {
                #region administrator
                System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
                if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
                {
                    Application.SetCompatibleTextRenderingDefault(false);

                    #region Limit run coutn
                    if (Limit.LimitUseCount(-1) == false)
                    {
                        Limit.Pass();
                    }
                    #endregion

                    Application.Run(new MainWindow());
                }
                else
                {
                    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                    startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
                    startInfo.Arguments = String.Join(" ", Args);
                    startInfo.Verb = "runas";
                    System.Diagnostics.Process.Start(startInfo);
                    System.Windows.Forms.Application.Exit();
                }
                #endregion
            }
            else
            {
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new MainWindow());
            }


        }
    }
}
